<?php

namespace app\admin\services;

use think\App;
use think\facade\Request;
use think\Service;
use think\facade\Session;
use app\admin\model\ClassificationModel;
use think\facade\Config;
class ClassificationService extends Service
{
    public function __construct(Request $request = null, App $app = null)
    {
        parent::__construct($request, $app);
           $this->ClassificationModel = new ClassificationModel();
    }

    /**
     * @param 登陆日志列表
     */
    public function userlist(){
        $post = Request::param();
        $page = isset($post['pageIndex'])?$post['pageIndex']:1;
        $limit = isset($post['pageSize'])?$post['pageSize']:10;
        if(!empty($post['username'])){
            $where = [['titlename','like','%'.$post['username'].'%']];
        }else{
            $where=[];
        }
        $list = $this->ClassificationModel->userlist($where,$page,$limit);
        $counts = $this->ClassificationModel->county($where);
        $array = [];
        foreach ($list as $value){
            $value['addTime'] = empty($value['addTime'])?"":$value['addTime'];
            $value['curl'] = empty($value['curl'])?"":$value['curl'];
            $value['updateTime'] = empty($value['updateTime'])?"":$value['updateTime'];
            $array[] = $value;
        }
        if(empty($list)){
            $data['rel'] = false;
            $data['msg'] = "暂无数据";
        }else{
            $data['rel'] = true;
            $data['msg'] = "获取成功";
        }
        $data['list'] = $array;
        $data['count'] = $counts;
        return $data;
    }


    /**
     * @param 添加分类
     */
    public function addlist(){
        if(!Request::isPost()){
            return $this->_getResponse("NO_POST");
        }
        $post = Request::param();
        if(empty($post['titlename'])){
            return $this->_getResponse("PARAM_EORRE");
        }
        $data = ['titlename'=> $post['titlename']];
        $rulst = $this->ClassificationModel->addlist($data);
        if(!$rulst){
            return $this->_getResponse("CAOZUO_ERROR");
        }
        return $this->_getResponse("USER_ADD");
    }


    /***
     * @param 修改分类
     */
    public function editlist(){
        if(!Request::isPost()){
            return $this->_getResponse("NO_POST");
        }
        $post = Request::param();
        if(empty($post['titlename'])){
            return $this->_getResponse("PARAM_EORRE");
        }
        if(empty($post['claid'])){
            return $this->_getResponse("PARAM_EORRE");
        }
        $urls = empty($post['urls'])?"":$post['urls'];
        $where = [['id','eq',$post['claid']]];
        $data = ['titlename'=>$post['titlename'],'curl'=>$urls];
        $rulst = $this->ClassificationModel->editlist($where,$data);
        if(!$rulst){
            return $this->_getResponse("CAOZUO_ERROR");
        }
        return $this->_getResponse("EDIT_SUCCESS");
    }


    /**
     * @param 删除分类
     */
    public function dellist(){
        if(!Request::isPost()){
            return $this->_getResponse("NO_POST");
        }
        $post = Request::param();
        if(empty($post['claid'])){
            return $this->_getResponse("PARAM_EORRE");
        }
        $where = [['id','eq',$post['claid']]];
        $this->ClassificationModel->dellist($where);
        return $this->_getResponse("DELETE_DATE");
    }

    /**
     * @param 开起关闭域名显示
     */
    public function shows(){
        if(!Request::isPost()){
            return $this->_getResponse("NO_POST");
        }
        $post = Request::param();
        if(empty($post['id'])){
            return $this->_getResponse("PARAM_EORRE");
        }
        if($post['steate']==1){
            $data = ['steate'=>2];
        }
        if($post['steate']==2){
            $data = ['steate'=>1];
        }
        $where = [['id','eq',$post['id']]];
        $rulst = $this->ClassificationModel->shows($where,$data);
        if(!$rulst){
            return $this->_getResponse("CAOZUO_ERROR");
        }
        return $this->_getResponse("ENBES_DATE");
    }

    /**
     * @param $key
     * @param array $data
     * @return mixed
     */
    protected function _getResponse($key, $data = [])
    {
        $maps = [
            "SUCCESS" => ["status" => 0, "msg" => "sccuess", "data" => $data],
            "USER_ADD" => ["status" => 0, "msg" => "添加成功", "data" => $data],
            "ENBES_DATE" => ["status" => 0, "msg" => "操作成功", "data" => $data],
            "DELETE_DATE" => ["status" => 0, "msg" => "删除成功", "data" => $data],
            "PARAM_EORRE" => ["status" => 1, "msg" => "参数错误", "data" => $data],
            "NUKNOW_EORRE" => ["status" => 2, "msg" => "未知错误", "data" => $data],
            "DATA_ERROR" => ["status" => 7, "msg" => "数据不存在", "data" => $data],
            "AUTH_EORRE" => ["status" => 3, "msg" => "权限不足", "data" => $data],
            "NO_POST" => ["status" => 4, "msg" => "非法请求", "data" => $data],
            "USER_ERROR" => ["status" => 5, "msg" => "用户名已存在", "data" => $data],
            "CAOZUO_ERROR" => ["status" => 6, "msg" => "操作失败", "data" => $data],
            "EDIT_SUCCESS" => ["status" => 0, "msg" => "修改成功", "data" => $data],
            "CONFIRM_SUCCESS" => ["status" => 0, "msg" => "确认成功", "data" => $data],
            "EMLIE_SUCCESS" => ["status" => 0, "msg" => "留言成功", "data" => $data]
        ];
        return $maps[$key];
    }
}